<?php

/**
 * This is the model class for table "taikhoan".
 *
 * The followings are the available columns in table 'taikhoan':
 * @property integer $id
 * @property string $tendangnhap
 * @property string $tenhienthi
 * @property string $matkhau
 * @property string $email
 */
class Taikhoan extends CActiveRecord
{
	public $nhac_lai_mat_khau;
	public $confirmNewPassword;
	public $currentPassword;
	public $newPassword;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'taikhoan';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('tendangnhap, tenhienthi, matkhau, email', 'required','message'=>'{attribute} chưa nhập dữ liệu'),
			array('tendangnhap', 'length', 'max'=>30),
			array('tenhienthi', 'length', 'max'=>20),
			array('matkhau', 'length', 'max'=>250),
			array('email', 'length', 'max'=>100),
			array('tendangnhap, email', 'unique', 'message'=>'{attribute} đã được sử dụng','on'=>'register'),
			array('email', 'email', 'message'=>'{attribute} không đúng định dạng'),
			array('nhac_lai_mat_khau', 'compare', 'compareAttribute'=>'matkhau', 'message' => "{attribute} không chính xác",'on'=>'register'),
			array('nhac_lai_mat_khau', 'safe'),
			array('currentPassword, newPassword, confirmNewPassword', 'required', 'message'=>'{attribute} không được để trống', 'on'=>'changePassword'),
			array('currentPassword, newPassword, confirmNewPassword', 'length','min'=>6, 'message'=>'{attribute} không ngắn quá', 'on'=>'changePassword'),
			array('currentPassword', 'checkPassword','on'=>'changePassword'),
			array('confirmNewPassword', 'compare', 'compareAttribute'=>'newPassword', 'message' => "{attribute} không chính xác", 'on'=>'changePassword'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, tendangnhap, tenhienthi, matkhau, email', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}
	public function checkPassword($attribute,$params)
	{
		$model = Taikhoan::model()->findByPk(Yii::app()->user->id);
		if ($this->MaHoaMatKhau($this->currentPassword)!=$model->matkhau){
			$this->addError($attribute, "Mật khẩu hiện tại không chính xác");
			return false;
		}
		return true;
	}
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'tendangnhap' => 'Tên đăng nhập',
			'tenhienthi' => 'Tên hiển thị',
			'matkhau' => 'Mật khẩu',
			'email' => 'Email',
			'nhac_lai_mat_khau'=>"Nhắc lại mật khẩu",
			'currentPassword'=>"Mật khẩu hiện tại",
			'newPassword'=>"Mật khẩu mới",
			'confirmNewPassword'=>'Nhắc lại mật khẩu mới'
		);
	}
	public static function MaHoaMatKhau($value)
	{
		return md5(sha1(base64_encode($value)));
	}
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('tendangnhap',$this->tendangnhap,true);
		$criteria->compare('tenhienthi',$this->tenhienthi,true);
		$criteria->compare('matkhau',$this->matkhau,true);
		$criteria->compare('email',$this->email,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
	            'pageSize'=>30,
	        ),
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Taikhoan the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
